<?php
header('Content-type:text/html;charset=utf8');
/**
 * xysc users Version1.0
 * ==================================
 * Copy 2016-2017
 * Web:www.xjh7.com
 * ==================================
 * Author:xjh
 * Date:2016-2-24
*/

class GoodsController extends Controller{
	
	/**
	 * 通过用户访问控制器过滤实现页面缓存
	 */
//	function filters(){
//		return array(
//			array(
//				'system.web.widgets.COutputCache + details',
//				'duration'=>1800,
//				'varyByParam'=>array('_goods_id'),//id变化 
//				
//			),//类过滤器
//		);	
//	}
	//列表页
	function actionCategory($brand_id = 0,$kkeyword = ''){
		$goods_model = Goods::model();
		//根据分类查询出品牌
		if(@$_POST['ttype'] == 'myAjax'){
		
			$brand_info = $goods_model->getBrandByCategoryId($_POST['id']);
			foreach ($brand_info as $_v){
				echo "<a href='./index.php?r=goods/category&brand_id=$_v[brand_id]'>".$_v['brand_name']."</a>";
			}
			exit;
		}
		$keyword = trim($kkeyword);
		if(isset($_POST['search'])){
			$keyword =trim($_POST['search']);
		}
	
		
		
		$brand_info = $goods_model -> getStoreName();
		$brandname = array();
		
		foreach($brand_info as $_v){
			$brandname[$_v['store_id']] = $_v['store_name'];
		}
		
		$infos = $goods_model -> pageSearch($brand_id,$_POST,$kkeyword);
		$category_info = $goods_model->getCategory();
		$this->render('list',array('category_info'=>$category_info,'infos'=>$infos,'brandname'=>$brandname,'keyword'=>$keyword));
	}
	
	//搜索功能
	function actionsearch($brand_id){
		$goods_model = new Goods();
		$infos = $goods_model -> pageSearch($brand_id);
	}

	//商品详细页面
	function actionDetails($_goods_id){
		
		//判断是否已经登录
		if(isset(Yii::app()->user->id)){
			$record_model = new Record();
			$record_model -> setRecord(Yii::app()->user->id,$_goods_id);
		}
		
		//通过缓存读取
		$goods_model = Goods::model();
//		$this->layout='//layouts/xy2';
		$info 		= $goods_model->getGoodsById($_goods_id);
		$rst  		= $goods_model->getGoodsDetailsImg($_goods_id);
		$store_info = $goods_model->getGoodsStore($_goods_id);
		$rec_info 	= $goods_model->getRecGoods($store_info[0]['store_id']);
		$styleinfo 	= $goods_model->getStyleInfo($_goods_id);
		
		//读取商品评论
		$eva_content = $goods_model->getEvaContentByGoodsId($_goods_id);
		
		$store_info = $store_info[0];
		$paraminfo	= $goods_model->getParamInfo($_goods_id);
		if($info['0']['active'] == 0){
			echo "该商品暂时不可用！";
		}else{
			$this->render('details',array('info'=>$info,'image'=>$rst[0],'store_info'=>$store_info,'rec_info'=>$rec_info,'styleinfo'=>$styleinfo,'paraminfo'=>$paraminfo,'goods_id'=>$_goods_id,'eva_content'=>$eva_content));
		}
		} 

	
}

	
?>